using System;
using System.Collections;
using System.Data;
using SecuBat.Crawling.Forms;
using Css.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace SecuBat.Notification
{
	/// <summary>
	/// Summary description for DatabaseManager.
	/// </summary>
	public class NotificationDbManager
	{
		public static int NewNotificationRun(String name, DateTime beginTime, DateTime endTime)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("newENotificationRun");
			db.AddInParameter(command, "@i_Name", DbType.String, name);
            db.AddInParameter(command, "@i_StartTime", DbType.DateTime, beginTime);
			if (endTime != DateTime.MinValue)
                db.AddInParameter(command, "@i_EndTime", DbType.DateTime, endTime);
			else
                db.AddInParameter(command, "@i_EndTime", DbType.DateTime, DBNull.Value);
            db.AddOutParameter(command, "@o_id", DbType.Int32, 4);

            if (db.ExecuteNonQuery(command) > 0)
			{
                return (int)db.GetParameterValue(command, "o_id");
			}
			return -1;
		}

		public static int NewNotification(int notificationRunId, int attackId, String domain, String pageUrl, String recipients, bool success, DateTime sendTime, String error)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand command = db.GetStoredProcCommand("newENotification");
			db.AddInParameter(command, "@i_IDNotificationRun", DbType.Int32, notificationRunId);
			db.AddInParameter(command, "@i_IDAttack", DbType.Int32, attackId);
			db.AddInParameter(command, "@i_Host", DbType.String, domain);
			db.AddInParameter(command, "@i_PageUrl", DbType.String, pageUrl);
			db.AddInParameter(command, "@i_Recipients", DbType.String, recipients);
			db.AddInParameter(command, "@i_Success", DbType.Boolean, success);
			if (sendTime != DateTime.MinValue)
				db.AddInParameter(command, "@i_SendTime", DbType.DateTime, sendTime);
			else
				db.AddInParameter(command, "@i_SendTime", DbType.DateTime, DBNull.Value);
			db.AddInParameter(command, "@i_Error", DbType.String, error);
			db.AddOutParameter(command, "@o_id", DbType.Int32, 4);

            if (db.ExecuteNonQuery(command) > 0)
			{
				return (int) db.GetParameterValue(command, "o_id");
			}
			return -1;
		}


	}
}
